package com.gongpingjialoan.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gongpingjialoan.R;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by Administrator on 2016/5/18.
 */
public class BrandPopRightAdapter extends BaseAdapter {

    Context mContext;
    LayoutInflater mLayoutInflater;
    JSONArray list;
    JSONObject jo;

    public BrandPopRightAdapter(Context context) {
        mContext = context;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setData(JSONArray list) {
        this.list = list;
        notifyDataSetChanged();
    }

    public void setParentData(JSONObject jo) {
        this.jo = jo;
        notifyDataSetChanged();
    }

    public JSONObject getParentData() {

        return jo;

    }

    @Override
    public int getCount() {
        if (list == null) {
            return 0;
        }
        return list.length();
    }

    @Override
    public JSONObject getItem(int position) {
        try {
            return list.getJSONObject(position);
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return null;
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = mLayoutInflater.inflate(R.layout.item_brand_pop_right_list, null);
        }

        JSONObject jo = getItem(position);
        TextView nameT = (TextView) convertView.findViewById(R.id.brandText);

        try {
            nameT.setText(jo.getString("model_slug_zh"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        TextView countT = (TextView) convertView.findViewById(R.id.count);

        try {
            countT.setText(jo.getString("count"));
        } catch (JSONException e) {
            e.printStackTrace();
        }
//        TextView nameT = (TextView) convertView.findViewById(R.id.name);

//        nameT.setText(list.get(position));


        return convertView;
    }
}

